package org.black.phoneUtility.sms.schedule.widget;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.black.phoneUtility.R;
import org.black.phoneUtility.sms.schedule.database.dataModel.SmsScheduleRecipient;
import org.black.phoneUtility.sms.schedule.provider.SmsScheduleContract;

import android.annotation.SuppressLint;
import android.content.Context;
import android.telephony.PhoneNumberUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SmsScheduleRecipientsAdapter extends BaseAdapter {
	private static final String LOG_TAG = SmsScheduleRecipientsAdapter.class
			.getName();

	final static class ViewHolder {
		RelativeLayout singleRowRelativelayout;
		TextView singleRowTitle;
		RelativeLayout doubleRowRelativeLayout;
		TextView doubleRowTitle;
		TextView doubleRowSummary;
	}

	private List<SmsScheduleRecipient> smsScheduleRecipients;
	private Map<Integer, Boolean> selectionItems;
	private Context context;
	private LayoutInflater layoutInflater;

	@SuppressLint("UseSparseArrays")
	public SmsScheduleRecipientsAdapter(Context context) {
		this.context = context;
		this.layoutInflater = LayoutInflater.from(this.context);
		this.smsScheduleRecipients = new ArrayList<SmsScheduleRecipient>();
		this.selectionItems = new HashMap<Integer, Boolean>();
	}

	public SmsScheduleRecipientsAdapter(Context context,
			List<SmsScheduleRecipient> smsScheduleRecipients) {
		this(context);
		this.smsScheduleRecipients = smsScheduleRecipients;
	}

	public void setSmsScheduleRecipients(
			List<SmsScheduleRecipient> smsScheduleRecipients) {
		this.smsScheduleRecipients = smsScheduleRecipients;
	}

	public boolean add(SmsScheduleRecipient smsScheduleRecipient) {
		if (this.smsScheduleRecipients == null) {
			this.smsScheduleRecipients = new ArrayList<SmsScheduleRecipient>();
		}
		boolean result = this.smsScheduleRecipients.add(smsScheduleRecipient);
		this.notifyDataSetChanged();
		return result;

	}

	public boolean add(List<SmsScheduleRecipient> smsScheduleRecipients) {
		boolean result = false;
		if (smsScheduleRecipients != null && this.smsScheduleRecipients != null) {
			result = this.smsScheduleRecipients.addAll(smsScheduleRecipients);
			this.notifyDataSetChanged();
		}
		return result;
	}

	public boolean remove(SmsScheduleRecipient smsScheduleRecipient) {
		boolean result = false;
		if (this.smsScheduleRecipients != null) {
			result = this.smsScheduleRecipients.remove(smsScheduleRecipient);
			this.notifyDataSetChanged();
		}
		return result;
	}

	public boolean remove(List<SmsScheduleRecipient> smsScheduleRecipiens) {
		boolean result = false;
		if (smsScheduleRecipiens != null && this.smsScheduleRecipients != null) {
			this.smsScheduleRecipients.removeAll(smsScheduleRecipiens);
			this.notifyDataSetChanged();
		}
		return result;
	}

	public void addSelection(int position, boolean checked) {
		this.selectionItems.put(position, checked);
		this.notifyDataSetChanged();
	}

	public void removeSelection(int position) {
		this.selectionItems.remove(position);
		this.notifyDataSetChanged();
	}

	public void removeSelections() {
		List<SmsScheduleRecipient> smsScheduleRecipients = new ArrayList<SmsScheduleRecipient>();
		Set<Integer> keys = this.selectionItems.keySet();
		for (Integer key : keys) {
			boolean checked = this.selectionItems.get(key);
			if (checked == true) {
				smsScheduleRecipients.add(this.smsScheduleRecipients.get(key));
			}
		}
		this.smsScheduleRecipients.removeAll(smsScheduleRecipients);
		this.notifyDataSetChanged();
	}

	public void clearSelection() {
		this.selectionItems.clear();
		this.notifyDataSetChanged();
	}

	public boolean isPositionChecked(int position) {
		Boolean result = this.selectionItems.get(position);
		return result == null ? false : result;
	}

	@Override
	public int getCount() {
		if (this.smsScheduleRecipients == null) {
			return 0;
		}

		return this.smsScheduleRecipients.size();
	}

	@Override
	public Object getItem(int position) {
		if (this.smsScheduleRecipients == null) {
			return null;
		}

		return this.smsScheduleRecipients.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		SmsScheduleRecipient smsScheduleRecipient = this.smsScheduleRecipients
				.get(position);
		if (convertView == null) {
			convertView = this.layoutInflater.inflate(
					R.layout.sms_schedule_recipients_list_row, null);
			viewHolder = new ViewHolder();
			viewHolder.singleRowRelativelayout = (RelativeLayout) convertView
					.findViewById(R.id.smsScheduleRecipientsRowSingleRowPanelRelativeLayout);
			viewHolder.singleRowTitle = (TextView) convertView
					.findViewById(R.id.smsScheduleRecipientsSingleRowTextView);
			viewHolder.doubleRowRelativeLayout = (RelativeLayout) convertView
					.findViewById(R.id.smsScheduleRecipientsTwoRowsPanelRelativeLayout);
			viewHolder.doubleRowTitle = (TextView) convertView
					.findViewById(R.id.smsScheduleRecipientsTwoRowsTitleTextView);
			viewHolder.doubleRowSummary = (TextView) convertView
					.findViewById(R.id.smsScheduleRecipientsTwoRowsSummaryTextView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		int type = smsScheduleRecipient.getType();
		switch (type) {
		case SmsScheduleContract.SmsScheduleRecipients.TYPE_USER_INPUT:
		case SmsScheduleContract.SmsScheduleRecipients.TYPE_CHOOSE_FROM_CONTACT:
		case SmsScheduleContract.SmsScheduleRecipients.TYPE_CHOOSE_FROM_CONTACT_GROUP:
			if (type == SmsScheduleContract.SmsScheduleRecipients.TYPE_USER_INPUT) {
				viewHolder.singleRowRelativelayout.setVisibility(View.VISIBLE);
				viewHolder.doubleRowRelativeLayout.setVisibility(View.GONE);
				String title = PhoneNumberUtils
						.formatNumber(smsScheduleRecipient.getPhoneNumber());
				viewHolder.singleRowTitle.setText(title);
			} else {
				viewHolder.singleRowRelativelayout.setVisibility(View.GONE);
				viewHolder.doubleRowRelativeLayout.setVisibility(View.VISIBLE);
				if (type == SmsScheduleContract.SmsScheduleRecipients.TYPE_CHOOSE_FROM_CONTACT) {
					viewHolder.doubleRowTitle.setText(smsScheduleRecipient
							.getName());
					viewHolder.doubleRowSummary
							.setText(PhoneNumberUtils
									.formatNumber(smsScheduleRecipient
											.getPhoneNumber()));
				} else {
					viewHolder.doubleRowTitle.setText(smsScheduleRecipient
							.getName()
							+ " ( "
							+ smsScheduleRecipient.getGroupName() + " ) ");
					viewHolder.doubleRowSummary
							.setText(PhoneNumberUtils
									.formatNumber(smsScheduleRecipient
											.getPhoneNumber()));
				}

			}
			break;
		default:
			Log.e(LOG_TAG, "type not support!, type: " + type);
			break;
		}
		if (this.selectionItems.get(position) != null) {
			convertView.setBackgroundColor(this.context.getResources()
					.getColor(android.R.color.holo_blue_light));
		} else {
			convertView.setBackground(this.context.getResources().getDrawable(
					android.R.drawable.list_selector_background));
		}
		return convertView;
	}
}
